package com.situ.mall.controller.front;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Category;
import com.situ.mall.entity.Product;
import com.situ.mall.entity.Result;
import com.situ.mall.entity.Suggest;
import com.situ.mall.entity.User;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.service.IProductService;
import com.situ.mall.service.IUserService;

@Controller
@RequestMapping("/home")
public class FrontHomeController {
	@Autowired
	private IProductService productService;
	@Autowired
	private ICategoryService categoryService;
	@Autowired
	private IUserService userService;
	
	@RequestMapping("/home.shtml")
	public String home(Model model) {
		System.out.println("FrontIndexController.index()");
		List<Category> topCategoryList = categoryService.selectTopCategoryList();
		List<Category> secondCategoryList = categoryService.selectSecondCategoryList();
		List<Product> listNew = selectNew(0, 5);
		List<Product> listCheap = selectCheap(0, 5);
		List<Product> listPhone = selectTop(0,6,100012);
		List<Product> listSkin = selectTop(0,6,100039);
		List<Product> listHearphone = selectTop(0,6,100038);
		
		model.addAttribute("listSkin", listSkin);
		model.addAttribute("listHearphone", listHearphone);
		model.addAttribute("listPhone", listPhone);
		model.addAttribute("listNew", listNew);
		model.addAttribute("listCheap", listCheap);
		model.addAttribute("topCategoryList", topCategoryList);
		model.addAttribute("secondCategoryList", secondCategoryList);
		return "Home";
	}
	
	@RequestMapping("/selectNew.shtml")
	@ResponseBody
	public List<Product> selectNew(@RequestParam(defaultValue = "0")Integer page, @RequestParam(defaultValue = "5")Integer limit) {
		System.out.println("FrontProductController.selectNew()");
		//查询相关新品
		List<Product> list = productService.selectNew(page, limit);
//		for (Product product : list) {
//			System.out.println(product);
//		}
		return list;
	}
	
	@RequestMapping("/selectCheap.shtml")
	@ResponseBody
	public List<Product> selectCheap(@RequestParam(defaultValue = "0")Integer page, @RequestParam(defaultValue = "5")Integer limit) {
		System.out.println("FrontProductController.selectCheap()");
		//查询相关新品
		List<Product> list = productService.selectCheap(page, limit);
		for (Product product : list) {
			System.out.println(product);
		}
		return list;
	}

	@RequestMapping("/selectTop.shtml")
	@ResponseBody
	public List<Product> selectTop(@RequestParam(defaultValue = "0")Integer page, @RequestParam(defaultValue = "5")Integer limit,Integer categoryId) {
		System.out.println("FrontProductController.selectTop()");
		//查询相关新品
		List<Product> list = productService.selectTop(page, limit,categoryId);
//		for (Product product : list) {
//			System.out.println(product);
//		}
		return list;
	}
	
	@RequestMapping("/suggest.shtml")
	@ResponseBody
	public Result suggest(String suggestText,HttpSession session) {
		System.out.println("FrontHomeController.suggest()");
		User user = (User)session.getAttribute("user");
		System.out.println("user = " + user);
		System.out.println("suggest = " + suggestText);
		Suggest suggest = new Suggest();
		suggest.setSuggest(suggestText);
		suggest.setUserId(user.getId());
		
		System.out.println("suggest =" + suggest);
		boolean isSuccess = userService.suggest(suggest);
		System.out.println("isSuccess = " + isSuccess);
		if(isSuccess) {
			return Result.success("感谢评论");
		}else {
			return Result.error("后台异常，请稍后评论");
		}
	}
}
